using System;
using System.Linq;
using System.Web.Mvc;
using Omu.Awesome.Mvc;
using Uber.Core;
using Uber.Core.Model;
using Uber.WebUI.Models;

namespace Uber.WebUI.Controllers
{
    public class ForumController : Controller
    {
        private const int ps = 3;
        public ActionResult Index()
        {
            var c = Fa.U.Where<Post>(o => o.ParentId == null).Count();
            var posts = Fa.U.Where<Post>(o => o.ParentId == null).OrderByDescending(o => o.CreationDate).Pag(1, ps);
            ViewData["m"] = c > ps;
            return View(posts);
        }

        public ActionResult Rows(int p)
        {
            var c = Fa.U.Where<Post>(o => o.ParentId == null).Count();
            var posts = Fa.U.Where<Post>(o => o.ParentId == null).OrderByDescending(o => o.CreationDate).Pag(p, ps);
            return Json(new { o = this.RenderView("rows", posts), m = c > p * ps });
        }

        [Authorize]
        public ActionResult AskQuestion()
        {
            return View(new AskQuestionInput());
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult AskQuestion(AskQuestionInput input)
        {
            if (!ModelState.IsValid) return View(input);
            var id = Fa.Post.Ask(input.Title, input.Body, Gl.UserId());
            return RedirectToAction("Question", new { id });
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult EditComment(AddCommentInput input)
        {
            try
            {
                if (!ModelState.IsValid) return Json(new { m = "v" });
                var c = Fa.Comment.Get(input.Id);
                if (!Gl.Allow(c.OwnerId)) return Json(new { m = "e" });
                Fa.Comment.Edit(input.Id, input.Body, Gl.UserId());
                return Json(new { m = "o" });
            }
            catch (Exception ex)
            {
                ex.Raize();
                return Json(new { m = "e" });
            }
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult EditPost(EditPostInput input)
        {
            try
            {
                if (!ModelState.IsValid) return Json(new { m = "v" });
                if (input.Id != 0)
                {
                    var q = Fa.Post.Get(input.Id);
                    if (q.ParentId == null)
                        if (q.Title == null || (q.Title.Length > 150 || q.Title.Length < 15))
                            return Json(new { m = "v" });

                    if (!Gl.Allow(q.OwnerId))
                        return Json(new { m = "e" });
                    Fa.Post.Edit(input.Id, input.Title, input.Body);
                    return Json(new { m = "o" });
                }
                var p = Fa.Post.Add(input.Body, Gl.UserId(), input.ParentId);
                return Json(new { m = "n", c = this.RenderView("q", p) });
            }
            catch (Exception ex)
            {
                ex.Raize();
                return Json(new { m = "e" });
            }
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult AddComment(AddCommentInput input)
        {
            try
            {
                if (!ModelState.IsValid) return Json(new { m = "v" });

                var c = Fa.Comment.Add(input.Id, Gl.UserId(), input.Body);

                return Json(new { m = "o", o = this.RenderView("comment", c) });
            }
            catch (Exception ex)
            {
                ex.Raize();
                return Json(new { m = "e" });
            }
        }

        public ActionResult Question(long id)
        {
            var q = Fa.Post.Get(id);
            return View(q);
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeletePost(long id)
        {
            try
            {
                var q = Fa.Post.Get(id);
                if (q.OwnerId != Gl.UserId() && !User.IsInRole("admin"))
                    return Json(new { m = "e" });
                Fa.Post.Delete(id);
                return Json(new { m = "o" });
            }
            catch (UberEx ex)
            {
                return Json(new { m = "v", c = ex.Message });
            }
            catch (Exception ex)
            {
                ex.Raize();
                return Json(new { m = "e" });
            }
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteComment(long id)
        {
            try
            {
                var c = Fa.Comment.Get(id);
                if (!Gl.Allow(c.OwnerId))
                    return Json(new { m = "e" });
                Fa.Comment.Delete(id);
                return Json(new { m = "o" });
            }
            catch (Exception ex)
            {
                ex.Raize();
                return Json(new { m = "e" });
            }
        }

        public ActionResult Questionn(long id, long nid)
        {
            Fa.Notification.Read(nid);
            return RedirectToAction("Question", new { id });
        }
    }
}